package com.kgc.contorller;


import com.alibaba.fastjson.JSONObject;
import com.kgc.entity.Role;
import com.kgc.entity.User;
import com.kgc.service.SmbmsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class SmbmsContorller {
    @Autowired
    private SmbmsService sc;

    @RequestMapping("/login")
    public String login(HttpSession session, String username, String password, Model model) throws ServletException, IOException {

        User user = sc.getUserByusernameAndpassword(username);

        String resultView = "forward:login.jsp";
        if (user == null){
            model.addAttribute("loginFlag","用户名不存在！");

        }else{
            if (!password.equals(user.getUserpassword())){
                model.addAttribute("passwordFlag","密码错误！");


            }else{
                resultView = "frame";
                //用户登录信息存放session
                session.setAttribute("loginUser",user);
            }
        }
        return resultView;
    }

    @RequestMapping("/out")
    public String out(){
        return "forward:login.jsp";
    }

    @RequestMapping("/toUserlist")
    public String toUserlist(){
        return "user/userlist";
    }


    @RequestMapping("/showUser")
    public void showUserlist(String name, String userRole, int currentPage, HttpServletResponse response){

        //获取列表数据
        List<User> list = sc.listUser(name,userRole,currentPage);
        System.out.println(userRole);
        //总记录数
        int totalCount = sc.countUsers(name,userRole);
        //总页数
        int totalPage = totalCount % 4 == 0 ? totalCount / 4 : totalCount / 4 + 1;



        Map<String,Object> map = new HashMap<>();
        map.put("list",list);
        map.put("totalCount",totalCount);
        map.put("totalPage",totalPage);

        String json = JSONObject.toJSONString(map);

        try {
            response.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();

            out.print(json);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/showRole")
    public void showUserlist(HttpServletResponse response){
        List<Role> list = sc.listRolename();
        String json = JSONObject.toJSONString(list);

        try {
            response.setCharacterEncoding("utf-8");
            PrintWriter out = response.getWriter();

            out.print(json);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/userview/{id}")
    public String selectuser(@PathVariable int id,Model model){
        User user = sc.selectUser(id);
        model.addAttribute("user",user);
        return "user/userview";
    }

    @RequestMapping("/toUseradd")
    public String toUserAdd(HttpSession session,@ModelAttribute("u") @Valid User u,Model model){
        List<Role> list = sc.listRolename();
        model.addAttribute("list",list);
        return "user/useradd";
    }

    @RequestMapping("/addUser")
    public String addUser(HttpSession session, User u, @RequestParam("cardphotoFile") MultipartFile cardphotoFile,@RequestParam("workphotoFile") MultipartFile workphotoFile,Model model){
        int flag = sc.insertUser(u);
        return "user/userlist";
    }


    @RequestMapping("/usercode")
    public String addUser(HttpSession session,String usercode){
        int flag = sc.countUserByUsercode(usercode);

        return JSONObject.toJSONString(flag);
    }


    @ExceptionHandler(Exception.class)
    public String handleException(){
        return "500";
    }



}
